package com.fh.controller.system.weixin.wxutil;


import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.fh.controller.system.weixin.wxmenu.Menu;

/**
 * 自定义菜单工具类
 * @author why
 *
 */
public class MenuUtil {
	
	//菜单创建(post)TokenThread.accessToken.getToken()调用token
	public final static String menu_create_url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
	
	/**
	 *创建菜单
	 * @param menu 菜单实例
	 * @param accessToken 凭证
	 * @return true 成功  false 失败
	 */
	public static boolean createMenu(Menu menu, String accessToken){
		boolean result = false;
		String url = menu_create_url.replace("ACCESS_TOKEN", accessToken);
		//将菜单对象转换成JSON字符串
		String jsonMenu = JSONObject.fromObject(menu).toString();
		//发起post请求创建菜单
		JSONObject jsonObject = WeixinUtil.httpRequest(url, "POST", jsonMenu);
		
		if(null != jsonObject){
			int errorCode = jsonObject.getInt("errcode");
			String errorMsg = jsonObject.getString("errmsg");
			System.out.println("====================="+errorCode+"     "+errorMsg);
			if(0 == errorCode){
				result = true;
			}else{
				result = false;
				System.out.println(errorCode+"     "+errorMsg);
			}
		}
		return result;
	}
}
